// TypeScript file
class startView extends egret.DisplayObjectContainer {
    public constructor() {
        super();
        this.width = 750;
        this.height = 1334;
        this.init();
    }

    private image: egret.Bitmap;

    private init() {
        this.initButton("重置图片对象", 20, 20, this.initImage);
        this.initButton("设置纹理", 20, 1000, this.setTexture);
        this.initButton("获取图片地址", 200, 1000, this.getVirtualUrl);
        this.initButton("拉伸填充", 400, 1000, this.SCALE);
        this.initButton("重复填充", 20, 1100, this.REPEAT);
        this.initButton("重复填充1", 200, 1100, this.REPEAT1);
        this.initButton("纹理集", 400, 1100, this.Texture);
        this.initButton("获取base64", 20, 1200, this.toDataURL);
        this.initButton("保存图片", 200, 1200, this.saveToFile);
        this.initButton("截图", 400, 1200, this.screenshot);
        this.initImage();
    }

    private renderTexture;

    //截图
    private screenshot() {
        var rt: egret.RenderTexture = new egret.RenderTexture;
        rt.drawToTexture(this, new egret.Rectangle(0, 0, 750, 1334));
        rt.saveToFile("image/png", "down.png", new egret.Rectangle(0, 0, 70, 70));
    }

    //保存图片
    private saveToFile() {
        this.image.texture = RES.getRes("1_jpeg");
        this.image.texture.saveToFile("image/png", "down.png", new egret.Rectangle(20, 20, 100, 100));
    }

    //获取base64
    private toDataURL() {
        this.image.texture = RES.getRes("1_jpeg");
        let a = this.image.texture.toDataURL("image/png", new egret.Rectangle(0, 0, 750, 750));
        console.log(a);
    }

    //纹理集
    private Texture() {
        var random = Math.random();
        if (random > 0.75) {
            this.image.texture = RES.getRes("timg_json#timg_1");
        } else if (random > 0.5) {
            this.image.texture = RES.getRes("timg_json#timg_2");
        } else if (random > 0.25) {
            this.image.texture = RES.getRes("timg_json#timg_3");
        } else {
            this.image.texture = RES.getRes("timg_json#timg_4");
        }
    }

    //拉伸填充
    private SCALE() {
        this.image.texture = RES.getRes("5_jpg");
        this.image.fillMode = egret.BitmapFillMode.SCALE;
        this.image.width *= 2;
        this.image.height *= 3;
    }
    //重复填充
    private REPEAT() {
        this.image.texture = RES.getRes("5_jpg");
        this.image.fillMode = egret.BitmapFillMode.REPEAT;
        this.image.width *= 2;
        this.image.height *= 3;
    }

    //重复填充
    private REPEAT1() {
        this.image.texture = RES.getRes("6_jpg");
        this.image.fillMode = egret.BitmapFillMode.REPEAT;
        this.image.width *= 2;
        this.image.height *= 3;
    }



    //测试
    private test() {
        console.log("测试")
    }

    //获取图片地址
    private getVirtualUrl() {
        var url = RES.getVirtualUrl("1_jpeg");
        console.log(url);
    }

    //设置图片纹理
    private setTexture() {
        var random = Math.random();
        if (random > 0.5) {
            this.image.texture = RES.getRes("1_jpeg");
        } else {
            this.image.texture = RES.getRes("1_jpg");
        }
    }

    //初始化
    private initImage() {
        if (this.image) {
            this.removeChild(this.image);
            this.image = null;
        }
        this.image = new egret.Bitmap;
        this.image.y = 100;
        this.addChild(this.image);
    }

    //通用按钮
    private initButton(textContent, x, y, callback) {
        var text: egret.TextField = new egret.TextField();
        text.text = textContent;
        text.textColor = 0x000000;
        text.background = true;
        text.backgroundColor = 0x03f425;
        text.x = x;
        text.y = y;
        text.touchEnabled = true;
        text.addEventListener(egret.TouchEvent.TOUCH_TAP, callback, this);
        this.addChild(text);
    }
}